package com.louis.kitty.admin.sevice.impl;

import java.util.*;

import com.louis.kitty.admin.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.louis.kitty.admin.dao.SysRoleMapper;
import com.louis.kitty.admin.dao.SysUserMapper;
import com.louis.kitty.admin.dao.SysUserRoleMapper;
import com.louis.kitty.admin.sevice.SysMenuService;
import com.louis.kitty.admin.sevice.SysUserService;
import com.louis.kitty.core.page.ColumnFilter;
import com.louis.kitty.core.page.MybatisPageHelper;
import com.louis.kitty.core.page.PageRequest;
import com.louis.kitty.core.page.PageResult;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Transactional
    @Override
    public int save(SysUser record) {
        Long id = null;
        if (record.getId() == null || record.getId() == 0) {
            // 新增用户
            sysUserMapper.insertSelective(record);
            id = record.getId();
        } else {
            // 更新用户信息
            sysUserMapper.updateByPrimaryKeySelective(record);
            sysUserRoleMapper.deleteByUserId(record.getId());
            id = record.getId();
            if (record.getUserRoles() != null && record.getUserRoles().size() > 0) {
                for (SysUserRole sysUserRole : record.getUserRoles()) {
                    sysUserRole.setUserId(id);
                }
                for (SysUserRole sysUserRole : record.getUserRoles()) {
                    //System.out.println(sysUserRole.getUserId()+'-'+sysUserRole.getRoleId());
                    sysUserRoleMapper.insertSelective(sysUserRole);
                }
            }else {
                sysUserRoleMapper.deleteByUserId(record.getId());
            }


        }
        // 更新用户角色
        if (id != null && id == 0) {
            return 1;
        }



//        if (record.getUserRoles() != null && record.getUserRoles().size() > 0) {
//            System.out.println(record.getUserRoles().toString());
//            // 为了防止非法篡改密码可以去掉
//            if (id != null) {
//                System.out.println("-------1---------");
////                for (SysUserRole sysUserRole : record.getUserRoles()) {
////                    sysUserRole.setUserId(id);
////                }
//
//
//
//            } else {
//                System.out.println("-------2---------");
////                sysUserRoleMapper.deleteByUserId(record.getId());
//            }
//
//            for (SysUserRole sysUserRole : record.getUserRoles()) {
//                System.out.println("-------3---------");
////                sysUserRoleMapper.insertSelective(sysUserRole);
//            }
//        }
        return 1;
    }

    @Override
    public int delete(SysUser record) {
        return sysUserMapper.deleteByPrimaryKey(record.getId());
    }

    @Override
    public int delete(List<SysUser> records) {
        for (SysUser record : records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public SysUser findById(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysUser findByName(String name) {
        return sysUserMapper.findByName(name);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        PageResult pageResult = null;
        String name = getColumnFilterValue(pageRequest, "name");
        String email = getColumnFilterValue(pageRequest, "email");
        if (name != null) {
            if (email != null) {
                pageResult = MybatisPageHelper.findPage(pageRequest, sysUserMapper, "findPageByNameAndEmail", name, email);
            } else {
                pageResult = MybatisPageHelper.findPage(pageRequest, sysUserMapper, "findPageByName", name);
            }
        } else {
            pageResult = MybatisPageHelper.findPage(pageRequest, sysUserMapper);
        }
        // 加载用户角色信息
        findUserRoles(pageResult);
        return pageResult;
    }

    /**
     * 获取过滤字段的值
     *
     * @param filterName
     * @return
     */
    public String getColumnFilterValue(PageRequest pageRequest, String filterName) {
        String value = null;
        ColumnFilter columnFilter = pageRequest.getColumnFilter(filterName);
        if (columnFilter != null) {
            value = columnFilter.getValue();
        }
        return value;
    }

    /**
     * 加载用户角色
     *
     * @param pageResult
     */
    private void findUserRoles(PageResult pageResult) {
        List<?> content = pageResult.getContent();
        for (Object object : content) {
            SysUser sysUser = (SysUser) object;
            List<SysUserRole> userRoles = findUserRoles(sysUser.getId());
            sysUser.setUserRoles(userRoles);
            sysUser.setRoleNames(getRoleNames(userRoles));
        }
    }

    private String getRoleNames(List<SysUserRole> userRoles) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<SysUserRole> iter = userRoles.iterator(); iter.hasNext(); ) {
            SysUserRole userRole = iter.next();
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(userRole.getRoleId());
            if (sysRole == null) {
                continue;
            }
            sb.append(sysRole.getRemark());
            if (iter.hasNext()) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }

    @Override
    public Set<String> findPermissions(String userName) {
        Set<String> perms = new HashSet<>();
        List<SysMenu> sysMenus = sysMenuService.findByUser(userName);
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getPerms() != null && !"".equals(sysMenu.getPerms())) {
                perms.add(sysMenu.getPerms());
            }
        }
        return perms;
    }

    @Override
    public List<SysUserRole> findUserRoles(Long userId) {
        return sysUserRoleMapper.findUserRoles(userId);
    }

    // 根据部门id获取部门人员
    @Override
    public List<SysUser> findUsersByDept(Long deptId) {
        List<SysUser> sysUsers = new ArrayList<SysUser>();
        sysUsers = sysUserMapper.findUsersByDept(deptId);
        return sysUsers;
    }

    // 根据角色id获取角色人员
    @Override
    public List<SysUser> findUsersByRole(List<Long> roleIds) {
        List<SysUser> sysUsers = new ArrayList<SysUser>();
        sysUsers = sysUserMapper.findUsersByRole(roleIds);
        return sysUsers;
    }

    @Override
    public List<SysUserVo> findAllUsers() {
        return sysUserMapper.findAllUsers();
    }

    public User findChargerByUserNo(String no) {
        User upperUser = sysUserMapper.findUpperUserByUserNoIfNoCharge(no);

        if (upperUser == null) {
            upperUser = sysUserMapper.findUpperByUserNoIfCharge(no);
        }

        if (upperUser != null && no.equals(upperUser.getNo())) {
            return null;
        } else if (upperUser == null) {
            return null;
        }

        return upperUser;
    }

    public User findUpperUserByUserNoIfNoCharge(String no){
        User upperUser = sysUserMapper.findUpperUserByUserNoIfNoCharge(no);
        return upperUser;
    }

    public User findUpperByUserNoIfCharge(String no){
        User upperUser = sysUserMapper.findUpperByUserNoIfCharge(no);
        return upperUser;
    }

    @Override
    public List<SysUser> findAllUsersByCompanyId(Integer companyId) {
        return sysUserMapper.findAllUsersByCompanyId(companyId);
    }



    public Map<Integer,User> findChargersByUserNo(String no) {
        Map<Integer,User> map=new HashMap<>();
        Integer level=0;
        User user=findChargerByUserNo(no);

        while (user!=null){
            map.put(++level, user);
            user=findChargerByUserNo(user.getNo());
        }
        return map;
    }


}
